package com.atguigu.gmall.product.controller;

import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.product.entity.*;
import com.atguigu.gmall.product.service.*;
import com.atguigu.gmall.product.vo.SaveSkuInfoVo;
import com.atguigu.gmall.product.vo.SkuDetailVo;
import com.atguigu.gmall.starter.cache.service.CacheService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(tags = "sku")
@RestController
@RequestMapping("/admin/product")
public class SkuController {

    @Autowired
    SpuImageService spuImageService;
    @Autowired
    SpuSaleAttrService spuSaleAttrService;
    @Autowired
    SpuSaleAttrValueService spuSaleAttrValueService;
    @Autowired
    SkuInfoService skuInfoService;
    @Autowired
    CacheService cacheService;

    //http://api.gmall.com/admin/product/spuImageList/{spuId}  spu_image
    @ApiOperation("根据spuId获取图片列表")
    @GetMapping("/spuImageList/{spuId}")
    public Result spuImageList(@PathVariable("spuId") Long spuId){
        List<SpuImage> list = spuImageService.lambdaQuery().eq(SpuImage::getSpuId, spuId).list();
        return Result.ok(list);
    }

    //http://api.gmall.com/admin/product/spuSaleAttrList/{spuId}  spu_sale_attr  spu_sale_attr_value
    //操作两张表  spu_sale_attr  spu_sale_attr_value
    @ApiOperation("根据spuId获取销售属性")
    @GetMapping("/spuSaleAttrList/{spuId}")
    public Result spuSaleAttrList(@PathVariable("spuId") Long spuId){
        //List<SpuSaleAttrValue> list = spuSaleAttrValueService.lambdaQuery().eq(SpuSaleAttrValue::getSpuId, spuId).list();
        // 修改方法完毕
        List<SpuSaleAttr> list = spuSaleAttrService.getSpuSaleAttrBySpuId(spuId);
        return Result.ok(list);
    }

    //http://api.gmall.com/admin/product/saveSkuInfo
    @ApiOperation("添加sku")
    @PostMapping("/saveSkuInfo")
    public Result saveSkuInfo(@RequestBody SaveSkuInfoVo saveSkuInfoVo){
        //todo 创建VO类 为什么保存的信息 skuId  BigDecimal price
        //分别操作四个表 新建VO
        skuInfoService.saveSkuInfo(saveSkuInfoVo);
        return Result.ok();
    }

    //http://api.gmall.com/admin/product/list/{page}/{limit}  sku_info
    @ApiOperation("获取sku分页列表")
    @GetMapping("/list/{page}/{limit}")
    public Result list(@PathVariable("page") Long page,@PathVariable("limit") Long limit){

        //写法1
        Page<SkuInfo> result = skuInfoService.lambdaQuery().page(new Page<>(page,limit));

        //写法2
        //Page<SkuInfo> result = skuInfoService.getPage(page,limit);

        return Result.ok(result);
    }

    //http://api.gmall.com/admin/product/onSale/{skuId}  sku_info  isSale是否销售（1：是 0：否）
    @ApiOperation("上架")
    @GetMapping("/onSale/{skuId}")
    public Result onSale(@PathVariable("skuId") Long skuId){
        skuInfoService.onSale(skuId);
        return Result.ok();
    }

    //http://api.gmall.com/admin/product/cancelSale/{skuId}
    @ApiOperation("下架")
    @GetMapping("/cancelSale/{skuId}")
    public Result cancelSale(@PathVariable("skuId") Long skuId){
        skuInfoService.cancelSale(skuId);
        return Result.ok();
    }

    //修改商品
    @GetMapping("/updateSkuInfo")
    public Result updateSkuInfo(@RequestBody SkuDetailVo vo){
        //修改数据库
        //删除缓存，延迟双删
        cacheService.delayDoubleDel("sku:info:49");

        return Result.ok();
    }
}
